#!/bin/bash

idchars0='a-zA-Z_'
idchars=$idchars0'0-9'
id="[$idchars0][$idchars]*"
pkgpath=$GOROOT/src/pkg/$1
pkgname=`basename $1 2>/dev/null`

# beginning of the current package's node
echo
echo @node $1
echo @chapter $pkgname
echo
echo @pkindex $pkgname
echo

# package documentation
godoc $1 2>/dev/null | sed 's/\t/    /g' | \
    sed 's/@/@@/g
s/{/@{/g
s/}/@}/g' | sed 's/ /@w{ }/g' | sed 's/^$/@sp 1/' | sed 's/.*/\0 @*\n/' | \
    sed 's/^var@w{ }\('$id'\)/@vindex \1\n\0/
s/^func@w{ }\(([^)]*)\)\?\(@w{ }\)*\('$id'\)/@findex \3\n\0/
s/^type@w{ }\('$id'\)/@tindex \1\n\0/
s/^const@w{ }\('$id'\)/@ctindex \1\n\0/' | \
    sed '/^const@w{ }(/,/^)/s/^\(@w{ }\)*\('$id'\)/@ctindex \2\n\0/
/^var@w{ }(/,/^)/s/^\(@w{ }\)*\('$id'\)/@vindex \2\n\0/' | \
    sed -n '0,/^SUBDIRECTORIES/p' | sed 's/^SUBDIRECTORIES.*//'
echo
echo

